var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var routes = require('./routes');

var app = express();

// view engine setup123
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(logger('dev'));
app.use(bodyParser.urlencoded({limit: '50mb'}));
app.use(bodyParser.text({defaultCharset: 'utf-8'}));
app.use(bodyParser.json({limit: '50mb'}));
app.use(session({
    name:"session_id",
    secret:"this_my_secretttt", // 建议使用 128 个字符的随机字符串
    cookie: { maxAge: 30*60*1000 }//session的最大时长
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/file',routes.FileRoute);
app.use('/order',routes.OrderRute);





app.get('/b',function(req,res){


});

app.get('/test',function(req,res){
    var beginTime = new Date().getTime();
    var Parser = require('parse-xl'),
        sample = new Parser('OTA.xlsx');

    var data =  sample.recordset(' MMLineFeed ',' MMNewLine ');
    console.log(new Date().getTime() - beginTime);

    var endTime = new Date().getTime();
    var DDArry =  new Array();

    var dataArray = JSON.stringify(data).split('MMNewLine');
    for(var i in dataArray){
        DDArry.push(dataArray[i].split('MMLineFeed'));
    }

    console.log(DDArry[5][0]);
    console.log(new Date().getTime() - endTime);

    console.log(DDArry[100][5]);
    res.send({"sd":"end"});
});

app.get('/a',function(req,res){
    res.send({"sd":"sd"})
});


// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}
  });
});


module.exports = app;


